package cn.tedu.eguser1.base.excption;

import cn.tedu.eguser1.base.response.JsonResult;
import cn.tedu.eguser1.base.response.StatusCode;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult doHandle(MethodArgumentNotValidException ex){
        String date = ex.getFieldError().getDefaultMessage();
        return new JsonResult(StatusCode.VALIDATEN_ERROR,date);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult doHandleConstraintViolationException(ConstraintViolationException ex){
        String date = ex.getMessage().split(":")[1].trim();
        return new JsonResult(StatusCode.VALIDATEN_ERROR,date);
    }


}
